package mao.exaimnation.littlerredbook;
import java.util.ArrayList;

/**
 * @author Totoro
 * @date 2020/8/30
 * @explain: 正则表达式，好好整理一下。
 */
public class Main2 {
    public static void main(String[] args) {
        String s = "mammm\naaa\n";
        String[] split = s.split("\n");
        for (int i = 0; i < split.length; i++) {
            System.out.println(split[i]);
        }
        System.out.println("++");
        String[] strings = slitMsg("aaaaaaaaaaa very long longlong.   aaaa\naaaaaaaaa\n");
        System.out.println(strings.length);
        for (int i = 0; i < strings.length; i++) {
            System.out.format("msg %d:%s",i,strings[i]);
        }
    }

    public static String[] slitMsg(String text){
        ArrayList<String> list = new ArrayList<>();
        String[] paragraph = text.split("\n");
        for (int i = 0; i < paragraph.length; i++) {
            //修改这里，补回换行符
            paragraph[i] = paragraph[i]+"\n";
            if (paragraph[i].length() <=1024){
                list.add(paragraph[i]);
            }else {
                String[] sentence = paragraph[i].split("\\.");
                StringBuilder builder = new StringBuilder();
                int j=0;
                while (j < sentence.length) {
                    if (builder.length()+sentence[j].length()+1 <=1024){
                        builder.append(sentence[j]);
                        builder.append(".");
                        j++;
                    }else {
                       list.add(builder.toString());
                       builder = new StringBuilder();
                    }
                }
                //修改这里
                list.add(builder.toString());
            }
        }
        String[] res  = new String[list.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = list.get(i);
        }
        return res;
    }
}
